<template>
  <div class="goods-comment-image">
    <!-- 图片列表 -->
    <div class="list">
      <a
        href="javascript:;"
        v-for="(url,index) in pictures"
        :key="url"
        @click="toggleImg(index)"
        :class="{active:curIndex === index}"
      >
        <img :src="url" alt="">
      </a>
    </div>
    <!-- 大图预览盒子 -->
    <div class="preview" v-if="hasPicture">
        <img :src="pictures[curIndex]" alt="">
        <!-- 关闭 按钮icon -->
        <i @click="closeFn" class="iconfont icon-close-new"></i>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue'
export default {
  name: 'GoodsCommentImage',
  props: {
    pictures: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    // 1. 点击小图  切换大图  小图激活状态变化
    // 2. 点击 X号 关闭大图
    const curIndex = ref(null) // 当前点击的是那一项
    const hasPicture = ref(false) // 声明一个响应式数据 控制大图是否显示
    function toggleImg (index) {
      curIndex.value = index
      hasPicture.value = true
    }

    // 当点击x号时
    function closeFn () {
      // 将大图隐藏
      hasPicture.value = false
      //   将初始值改为null  使该标签对应的样式置空
      curIndex.value = null
    }

    return {
      toggleImg,
      curIndex, // 当前点击的是那一项
      hasPicture,
      closeFn
    }
  }
}
</script>
<style scoped lang="less">
.goods-comment-image {
  .list {
    display: flex;
    flex-wrap: wrap;
    margin-top: 10px;
    a {
      width: 120px;
      height: 120px;
      border:1px solid #e4e4e4;
      margin-right: 20px;
      margin-bottom: 10px;
      img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
      &.active {
        border-color: @xtxColor;
      }
    }
  }
  .preview {
      width: 480px;
      height: 480px;
      border: 1px solid #e4e4e4;
      background: #f8f8f8;
      margin-bottom: 20px;
      position: relative;
      img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
      .icon-close-new {
        position: absolute;
        right: 0;
        top: 0;
        width: 30px;
        height: 30px;
        background: rgba(0, 0, 0, 0.2);
        color: #fff;
        text-align: center;
        line-height: 30px;
        cursor: pointer;
      }
    }
}
</style>
